﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;

    public class NoteReceiverSpecification : ISpecification<NoteReceiver, NoteReceiverSearch>
    {
        #region ISpecification<Note,NoteSearch> Members

        public NoteReceiverSearch SearchParams
        {
            get;
            set;
        }

        #endregion

        #region ISpecification<Note> Members

        public System.Linq.Expressions.Expression<Func<NoteReceiver, bool>> Expression
        {
            get
            {
                IQueryBuilder<NoteReceiver> queryBuilder = QueryBuilder.Create<NoteReceiver>();

                if (null != SearchParams)
                {
                    if (SearchParams.NoteID > 0 && SearchParams.ReceiverID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(nr => nr.Note.ID, SearchParams.NoteID);
                        queryBuilder = queryBuilder.Equals(nr => nr.ReceiverUserAccount.ID, SearchParams.ReceiverID);
                    }
                    else if (SearchParams.ReceiverID > 0)
                    {
                        queryBuilder = queryBuilder.Equals(nr => nr.ReceiverUserAccount.ID, SearchParams.ReceiverID);
                    }
                    if (SearchParams.IsNew.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(nr => nr.IsNew, SearchParams.IsNew.Value);
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
        #endregion
    }
}
